<?php


namespace app\admin\validate;


use think\facade\Db;
use think\Validate;

class DlUser extends Validate
{
    protected $rule = [
        'mobile'           =>  'require|checkUser',
        'jl_bili'          =>  'require|float',
        'did'              =>  'require',
        'longitude'          =>  'require',
        'latitude'          =>  'require',
    ];
    protected $message = [
        'mobile.require'        => '请输入手机号码',
        'jl_bili.require'       => '请输入奖励比例,不需要填写0!',
        'jl_bili.float'         => '奖励比例只能为数字',
        'did.require'           => '请选择地区',
        'longitude.require'     => '请选择经度',
        'latitude.require'      => '请选择纬度',
    ];
    //检查用户
    protected function checkUser($value, $rule, $data){
        $user=get_user_info($value,1);
        if (empty($user)){
            return '账户不存在';
        }
        //判断是否已添加过
        $count_where[]=['level','=',$data['level']];
        $count_where[]=['user_id','=',$user['user_id']];
        if ($data['id']){
            $count_where[]=['id','<>',$data['id']];
        }
        $count=Db::name('dl_user')->where($count_where)->count();
        if ($count>0){
            return "该用户已添加过此代理";
        }
//        //判断用户是否已购买过当前代理产品
//        $order_where[]=['dl_level','=',$data['level']];
//        $order_where[]=['order_status','>',0];
//        $order_where[]=['user_id','=',$user['user_id']];
//        $order_count=Db::name('jifen_order')->where($order_where)->count();
//        if ($order_count==0){
//            return "该用户尚未购买此代理产品";
//        }
        return true;
    }
}